package com.summer.container;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Stack;
import java.util.Vector;

import com.summer.container.SafeIterableList.Node;

public class ListTest {
	
	public static void main(String[] args) throws InterruptedException {
//		iteratorWithAdditionsTest();
		
		ArrayList<String> list = new ArrayList<String>();
		
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		list.add("e");
		list.add("f");
		list.add("g");
//		list.add("h");
		
		reverse(list);
		
		for(String s : list) {
			System.out.println(s);
		}
		
	} 

	public static <T extends List> T reverse(T list) {
    	int first = 0;
    	int tail = list.size() - 1;
        int middle = tail >> 1;

        for(; first <= middle; first++,tail--) {
            Object swap = list.get(first);
            list.set(first, list.get(tail));
            list.set(tail, swap);
        }

        return list;
    }
	
	
	private static void testTake() {
		SafeIterableList<String> list = new SafeIterableList<>();
		ArrayList<String> dstList = new ArrayList<>();
		for(int i = 0; i<1000;i++ ){
			String str = String.valueOf(i);
			list.add(str);
			dstList.add(str);
		}
		
		Random random = new Random();
		int index = random.nextInt(1000);
		
		String str1 = list.get(index).item;
		String str2 = dstList.get(index);
		if(str1.equals(str2)) {
			System.out.println("list  " + str1 + " " + index);
			System.out.println("dstList  " + str1 + " " + index);
		}
		
	}
	
	private static void testAdd() {
//		ArrayDeque<String> list = new ArrayDeque<String>();
		SafeIterableList<String> list = new SafeIterableList<>();
		ArrayList<String> dstList = new ArrayList<>();
		for(int i = 0; i<1000;i++ ){
			String str = String.valueOf(i);
			list.add(str);
			dstList.add(str);
		}
		for(int i = 10; i<20; i++) {
			list.remove(i);
			dstList.remove(i);
		}
		for(int i = 0; i<990;i++ ){
			if(!list.get(i).item.equals(dstList.get(i))) {
				System.out.println("不同");
			}
		}
//		for(Node<String> node : list) { 
//			System.out.println("list  "+node.item);
//		}
		for(String node : dstList) { 
			System.out.println("dstList  "+node);
		}
	}
	
	private static void iteratorWithAdditionsTest() throws InterruptedException {
		SafeIterableList<String> list = new SafeIterableList<>();

		Object lock = new Object[0];
		
		new Thread(() -> {
			long ss = 0;
			for(int i = 0; i<1000;i++ ){
				list.add(String.valueOf(i));
				try {
					if(300==i) {
						Thread.currentThread().sleep(2000);
					}
					if(100<i) {
						Thread.currentThread().sleep(2);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ss+=i;
			}
			
			System.out.println("ssss==========="+ss);
			synchronized(lock) {
				lock.notifyAll();
			}
		}).start();
		
		new Thread(() -> {
			Iterator<Node<String>> it = list.iteratorWithAdditions();
			
			long sum = 0;
			
			while(it.hasNext()) {
				String str = it.next().item;
//				System.out.println(str);
				sum += Long.parseLong(str);
				try {
					Thread.currentThread().sleep(5);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("count sum =========== " + sum);
			
		}).start();
		
//		new Thread(() -> {
//			Iterator<Node<String>> it = list.iterator();
//			while(it.hasNext()) {
//				System.out.println("iterator ===  "+it.next().item);
//				try {
//					Thread.currentThread().sleep(5);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}).start();
		
		synchronized(lock) {
			lock.wait();
		}
		
	}
	
	private static void asyncTest() throws InterruptedException {
//		LinkedList<String> list  = new LinkedList<String>();
		ArrayDeque<String> list  = new ArrayDeque<String>();
//		SafeIterableList list = new SafeIterableList();
		
		Object lock = new Object[0];
		
		
		int count = 99;
		System.out.println("count "+ count);
		System.out.println("count "+ (count>>1));
		
		
		new Thread(() -> {
			for(int i = 0; i<1000;i++ ){
				list.add(String.valueOf(i));
				try {
					if(100<i) {
						Thread.currentThread().sleep(2);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			synchronized(lock) {
				lock.notifyAll();
			}
		}).start();		
		
		new Thread(() -> {
			for(int i = 0; i<1000;i++ ){
				list.remove(i);
				try {
					if(50<i) {
						Thread.currentThread().sleep(2);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			synchronized(lock) {
				lock.notifyAll();
			}
		}).start();		
		
		new Thread(() -> {
			for(String str : list){
				System.out.println(str);
			}
		}).start();
		
		new Thread(() -> {
			Iterator<String> it = list.iterator();
			while(it.hasNext()) {
				System.out.println(it.next());
				try {
					Thread.currentThread().sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
		
		synchronized(lock) {
			lock.wait();
		}
	}
	
}
